package me.ywy.apt.ormdb.sqlite;

import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import me.ywy.apt.ormdb.sqlite.impl.BaseDao;

/**
 * Created by ywy on 2018-04-12.
 */
public class BaseDaoFactory {
    private static final BaseDaoFactory ourInstance = new BaseDaoFactory();
    private final SQLiteDatabase sqLiteDatabase;

    public static BaseDaoFactory getInstance() {
        return ourInstance;
    }

    private BaseDaoFactory() {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/db_ywy.db";
        sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(path, null);
    }

    public synchronized <T> BaseDao<T> getBaseDao(Class<T> eneieyClass){

        BaseDao baseDao = null;

        try {
            baseDao = BaseDao.class.newInstance();
            baseDao.init(eneieyClass , sqLiteDatabase);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return baseDao;
    }
}
